# -*- coding: utf-8 -*-
###############################################################################
# file    : models.py
# author  : wangyubin
# date    : Tue Apr 15 20:27:42 2014
# 
# brief   : models for bug away
# history : init
###############################################################################

from uliweb.orm import *
from uliweb import settings

#----------------------------
# 用户信息
#----------------------------
class User(Model):
    __tablename__ = 't_user'

    name          = Field(str, max_length=128, required=True)
    real_name     = Field(str, max_length=128, required=True)
    passwd        = Field(str, max_length=128, required=True)
    email         = Field(str, max_length=128)
    telephone     = Field(str, max_length=16)
    role          = Field(int, required=True, default=settings.PERMISSION.NORMAL)

    
#----------------------------
# 项目信息
#----------------------------
class Project(Model):
    __tablename__ = 't_project'

    name          = Field(str, max_length=100, required=True)
    desc          = Field(TEXT)

    users         = ManyToMany('t_user', collection_name='projects')
    update_time   = Field(datetime.datetime, auto_now_add=True, auto_now=True, verbose_name='更新时间')
    create_time   = Field(datetime.datetime, auto_now_add=True, verbose_name='创建时间')
    create_user   = Reference('t_user', required=True, collection_name='user_projects')
    
#----------------------------
# master信息
#----------------------------
class Bug_Importance(Model):
    __tablename__ = 't_bug_importance'

    name          = Field(str, max_length=256, required=True, verbose_name='重要性名称')
    code          = Field(int, required=True, verbose_name='重要性code')


class Bug_State(Model):
    __tablename__ = 't_bug_state'

    name          = Field(str, max_length=256, required=True, verbose_name='对应状态名称')
    code          = Field(int, required=True, verbose_name='对应状态code')

#----------------------------
# bug信息
#----------------------------
class Bug(Model):
    __tablename__  = 't_bug'

    title            = Field(str, max_length=256, required=True, verbose_name='bug标题')
    desc             = Field(TEXT, required=True, verbose_name='bug描述')

    bug_importance   = Reference('t_bug_importance', required=True,
                                 collection_name='bugs', verbose_name='bug重要性')
    bug_state        = Reference('t_bug_state', required=True, collection_name='bugs', verbose_name='bug状态')
    project          = Reference('t_project', required=True, collection_name='bugs', verbose_name='bug所属项目')
    
    update_time      = Field(datetime.datetime, auto_now_add=True, auto_now=True, verbose_name='更新时间')
    create_time      = Field(datetime.datetime, auto_now_add=True, verbose_name='创建时间')
    assign_from_user = Reference('t_user', collection_name='assigning_bugs', verbose_name='指定bug的对应者的人')
    assign_to_user   = Reference('t_user', collection_name='assigned_bugs', verbose_name='对应bug的人')
    create_user      = Reference('t_user', collection_name='created_bugs',
                                 required=True, verbose_name='创建bug的用户')


class Bug_Attachment(Model):
    __tablename__ = 't_bug_attachment'

    name          = Field(str, max_length=256, required=True, verbose_name='附件文件名')
    path          = Field(str, max_length=256, required=True, verbose_name='附件路径')
    
    bug           = Reference('t_bug', collection_name='bug_attachments')


class Bug_Comment(Model):
    __tablename__ = 't_bug_comment'

    comment       = Field(TEXT, required=True, verbose_name='comment')
    create_time   = Field(datetime.datetime, auto_now_add=True, auto_now=True, verbose_name='创建时间')
    update_time   = Field(datetime.datetime, auto_now_add=True, auto_now=True, verbose_name='更新时间')
    create_user   = Reference('t_user', collection_name='create_comments', required =True, verbose_name='创建comment的用户')
    update_user   = Reference('t_user', collection_name='update_comments', required =True, verbose_name='更新comment的用户')

    bug           = Reference('t_bug', collection_name='bug_comments')


class Bug_History(Model):
    __tablename__ = 't_bug_history'
    
    title            = Field(str, max_length=256, required=True, verbose_name='bug标题')
    desc             = Field(TEXT, required=True, verbose_name='bug描述')

    bug_importance   = Reference('t_bug_importance', required=True, verbose_name='bug重要性')
    bug_state        = Reference('t_bug_state', required=True, verbose_name='bug状态')
    project          = Reference('t_project', required=True, verbose_name='bug所属项目')
    
    update_time      = Field(datetime.datetime, auto_now_add=True, auto_now=True, verbose_name='更新时间')
    create_time      = Field(datetime.datetime, auto_now_add=True, verbose_name='创建时间')
    assign_from_user = Reference('t_user', verbose_name='指定bug的对应者的人')
    assign_to_user   = Reference('t_user', verbose_name='对应bug的人')
    create_user      = Reference('t_user', required=True, verbose_name='创建bug的用户')

    bug              = Reference('t_bug', required=True, collection_name="bug_histories")


